//
//  ViewController.m
//  JSCDemo
//
//  Created by QHIEX on 16/4/21.
//  Copyright © 2016年 QHIEX. All rights reserved.
//

#import "ViewController.h"
#import "JSTemp.h"
#import "JSTempProtocol.h"
@interface ViewController ()<UIWebViewDelegate,JSTempProtocol>

{
    JSContext *context;
}
/** webView */
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setHidden:YES];
    
    _webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    _webView.delegate =self;
    [self.view addSubview:_webView];
    /*
    NSString *path = [[NSBundle mainBundle] pathForResource:@"webapp" ofType:@""];
    NSString *urlStr = [NSString stringWithFormat:@"file://%@/modules/html/examples/index.html",path];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
     */
    NSString *path = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"html"];
    NSString *urlStr = [NSString stringWithFormat:@"file://%@",path];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    
    
}


#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
     context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    JSTemp *temp = [[JSTemp alloc] init];
    context[@"temp"] = temp;
    context[@"nativeData"] = @{@"student":@"dabiaoge",@"age":@"11",@"grade":@"1"};
    temp.context = context;
    temp.controller = self;
    /*
    context[@"temp"] = self;
     */
    context.exceptionHandler = ^(JSContext *context, JSValue *exception){
        NSLog(@" error = %@",exception.description);
    };
    
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{

}


#pragma mark -Procotal
- (void)JSsummationAndPrintA:(int)a withB:(int)b withC:(int)c
{
    NSLog(@"(%d) a= %d, b= %d, c = %d",__LINE__,a,b,c);
}

- (void)JSsummation:(NSString *)message
{
    NSLog(@"%s",__FUNCTION__);
}



@end
